package ar.edu.itba.it.pdc.configuration.models.filters;

import ar.edu.itba.it.pdc.configuration.models.Request;
import ar.edu.itba.it.pdc.configuration.models.Response;
import ar.edu.itba.it.pdc.configuration.models.filters.destinyFilters.DestinyFilter;
import ar.edu.itba.it.pdc.configuration.models.filters.originFilters.OriginFilter;

public class FilterRule {

	private int id;
		
	private OriginFilter originFilter;
	
	private DestinyFilter destinyFilter;
	
	public FilterRule() {
	}
	
	public FilterRule(int id, DestinyFilter dest, OriginFilter origin) {
		this.id = id;
		this.originFilter = origin;
		this.destinyFilter = dest;
	}
	
	public int getId() {
		return id;
	}
	
	@Override
	public String toString() {
		return "Filter Rule #" + getId() + "\nRequest Criteria: " + 
				originFilter.getCriteria() + "\nRequest Content: " +
				originFilter.getContent() + "\nResponse Criteria: " +
				destinyFilter.getCriteria() + "\nResponse Content: " +
				destinyFilter.getContent();
	}
	
	public DestinyFilter getDestinyFilter() {
		return destinyFilter;
	}
	
	public OriginFilter getOriginFilter() {
		return originFilter;
	}

	public boolean applyRequestFilter(Request req) {
		if (destinyFilter.isApplyOnRequest()) {
			return originFilter.applyFilter(req) || destinyFilter.applyFilterOnRequest(req);
		}
		return true;
	}

	public boolean applyResponseFilter(Request req, Response resp) {
		if (destinyFilter.isApplyOnResponse()) {
			return originFilter.applyFilter(req) || destinyFilter.applyFilterOnResponse(resp);
		}
		return true;
	}
}